package com.probiz.estore.common.service.impl;

import java.util.List;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.customer.Address;
import com.probiz.estore.common.model.customer.Customer;
import com.probiz.estore.common.model.customer.Membership;
import com.probiz.estore.common.model.customer.WishlistItem;
import com.probiz.estore.common.service.CustomerService;
import com.probiz.estore.customer.service.AddressManager;
import com.probiz.estore.customer.service.CustomerManager;
import com.probiz.estore.customer.service.MembershipManager;
import com.probiz.estore.customer.service.ReviewEligibilityManager;
import com.probiz.estore.customer.service.WishlistItemManager;
import com.probiz.estore.customer.service.WishlistManager;
import com.probiz.estore.webapp.util.RequestContext;

/**
 * 前台用户服务类
 *
 */
public class CustomerServiceImpl implements CustomerService {
	private CustomerManager customerManager;
	private AddressManager addressManager;	
    private WishlistManager wishlistManager;
    private WishlistItemManager wishlistItemManager;
    private MembershipManager membershipManager;
    private ReviewEligibilityManager reviewEligibilityManager;
    
	public void setMembershipManager(MembershipManager membershipManager) {
		this.membershipManager = membershipManager;
	}

	public void setWishlistItemManager(WishlistItemManager wishlistItemManager) {
		this.wishlistItemManager = wishlistItemManager;
	}

	public void setWishlistManager(WishlistManager wishlistManager) {
		this.wishlistManager = wishlistManager;
	}

	public void setCustomerManager(CustomerManager customerManager) {
		this.customerManager = customerManager;
	}

	public List<Address> getAllShippingAddressByAppuserId(Integer appuserId) {
		return addressManager.getAllShippingAddressByAppuserId(appuserId);
	}

	public Address getDefShippingAddr(Integer appuserId) {
		return addressManager.getDefShippingAddr(appuserId);
	}
	
	public Customer getByAppUserId(Integer appUserId) {
		return this.customerManager.getByAppUserId(appUserId);
	}

	public WishlistItem getWishlistItem(Integer wishlistItemId) {
		return this.wishlistItemManager.getById(wishlistItemId);
	}

	public void saveCustomerAddAsDef(Address address) {
		addressManager.save(address);
	}

	public void setReviewEligibilityManager(
			ReviewEligibilityManager reviewEligibilityManager) {
		this.reviewEligibilityManager = reviewEligibilityManager;
	}

	public void updateAddressAsDefault(Integer addressId) {
		if(RequestContext.isAnonymousUser())return;
		addressManager.updateDefShippingAddr(RequestContext.getCurrentUserId(),addressId);
	}

	public void delAddress(Integer addressId) {
		if(RequestContext.isAnonymousUser())return;
		addressManager.deleteById(addressId);
		addressManager.updateDefShippingAddr(RequestContext.getCurrentUserId(),null);
	}

	public void setWishListAsDefault(Integer wishListId) {
		if(wishListId != null && !RequestContext.isAnonymousUser()){
				wishlistManager.updateDefaultWishlist(wishListId, RequestContext.getCurrentUserId());
		}
	}

	public void setAddressManager(AddressManager addressManager) {
		this.addressManager = addressManager;
	}

	public List<Membership> getMembershipByName(String membershipName) {
		return membershipManager.getMembershipByName(membershipName);
	}


	public void saveCustomer(Customer customer) {
		customerManager.save(customer);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.common.service.CustomerService#addEligibility(java.lang.Integer, java.lang.Integer)
	 */
	public void addEligibility(Integer customerId,Integer productId){
		reviewEligibilityManager.addEligibility(customerId, productId);
	}

	public Long countCustomerTotal(){
		return customerManager.countCustomerTotal();
	}
	
}
